﻿using JV.Com.Business;
using JV.Com.Internals;
using JV.Com.Model;
using JV.Com.Tools;
using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace JV.Com.ViewModels
{
    public sealed class FolderPageViewModel : PageBaseViewModel
    {
        private Folder m_folder;
        public FolderPageViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            m_folder = new Folder();
        }

        private int m_folderId;
        public async override void OnNavigatedTo(object navigationParameter, Windows.UI.Xaml.Navigation.NavigationMode navigationMode, Dictionary<string,object> viewModelState)
        {
 	        base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);
            m_folderId = (int)navigationParameter;
            if(navigationMode != Windows.UI.Xaml.Navigation.NavigationMode.Back)
            {
                FolderPage = 0;
            }

            await LoadFolderAsync(m_folderId);
        }

        private async Task LoadFolderAsync(int id)
        {
            m_folder = await Service.GetFolderAsync(id);

            var converter = new HtmlToRichTextConverter(HtmlToRichTextConverter.ConvertType.Folder);
            var inlines = converter.GetHTMLInlineCollection(m_folder.Content);
            m_folder.NbPages = inlines.TotalPageFolder;

            OnPropertyChanged("SmallImageUrl");
            OnPropertyChanged("PageTitle");
        }

        public string SummaryPage
        {
            get
            {
                return FolderPage == 0 ? "Sommaire" : string.Format("Page {0} / {1}", FolderPage, m_folder.NbPages);
            }
        }
        public string SmallImageUrl { get { return m_folder.SmallImageUrl; } }
        public override string PageTitle { get { return m_folder.Title; } }

        private INavigationService m_folderNavigationService;
        public INavigationService FolderNavigationService
        {
            get { return m_folderNavigationService; }
            set
            {
                if (m_folderNavigationService == null)
                {
                    m_folderNavigationService = value;
                    m_folderNavigationService.Navigate("DetailFolder", GetSerializedParameter(FolderPage));
                }
            }
        }

        private int m_folderPage;
        [RestorableState]
        public int FolderPage
        {
            get { return m_folderPage; }
            set { SetProperty<int>(ref m_folderPage, value); }
        }

        public void RaiseAllProperties()
        {
            OnPropertyChanged("GoPrevPageCommand");
            OnPropertyChanged("GoNextPageCommand");
            OnPropertyChanged("GoSummaryCommand");
            OnPropertyChanged("SummaryPage");
        }

        private bool m_canGoNextPage;
        public bool CanGoNextPage
        {
            get { return m_canGoNextPage; }
            set { SetProperty<bool>(ref m_canGoNextPage, value); }
        }

        private bool m_canGoPrevPage;
        public bool CanGoPrevPage
        {
            get { return m_canGoPrevPage; }
            set { SetProperty<bool>(ref m_canGoPrevPage, value); }
        }

        public ICommand GoSummaryCommand
        {
            get
            {
                return new DelegateCommand(() =>
                    FolderNavigationService.Navigate("DetailFolder", GetSerializedParameter(0)),
                    () => CanGoPrevPage);
            }
        }

        public ICommand GoNextPageCommand
        {
            get
            {
                return new DelegateCommand(() =>
                    FolderNavigationService.Navigate("DetailFolder", GetSerializedParameter(++FolderPage)),
                    () => CanGoNextPage);
            }
        }

        public ICommand GoPrevPageCommand
        {
            get
            {
                return new DelegateCommand(() =>
                    FolderNavigationService.Navigate("DetailFolder", GetSerializedParameter(--FolderPage)),
                    () => CanGoPrevPage);
            }
        }

        private string GetSerializedParameter(int page)
        {
            var parameter = new FolderPageNavigationParameter()
            {
                FolderId = m_folderId,
                FolderPage = page
            };
            return SerializerHelper.Serialize<FolderPageNavigationParameter>(parameter);
        }
    }
}
